//
//  UITabBarController+Lc.m
//  18Birdies
//
//  Created by weihua on 2016/12/15.
//  Copyright © 2016年 Lc. All rights reserved.
//

#import "UITabBarController+Lc.h"
#import "UINavigationController+Lc.h"

const NSNotificationName UITabBarControllerReClicked = @"UITabBarControllerReClicked";
const NSNotificationName UITabBarControllerSelected = @"UITabBarControllerSelected";
const NSString* UITabBarControllerSelectedIndexKey = @"UITabBarControllerSelectedIndex";
const NSString* UITabBarControllerKey = @"UITabBarController";

@implementation UITabBarController (Lc)

- (__kindof UIViewController*)viewControllerAtIndex:(NSUInteger)tabIndex {
  if (tabIndex < self.viewControllers.count) {
    return self.viewControllers[tabIndex];
  }
  return nil;
}

- (void)switchToTabIndex:(NSUInteger)tabIndex
      pushViewController:(UIViewController*)toViewController {
  [self switchToTabIndex:tabIndex
      pushViewController:toViewController
                animated:YES];
}

- (void)switchToTabIndex:(NSUInteger)tabIndex
      pushViewController:(UIViewController*)toViewController
                animated:(BOOL)animated {
  if (self.presentedViewController != nil) {
    [self dismissViewControllerAnimated:NO completion:nil];
  }
  UINavigationController* oldNavigationViewController = nil;
  if (tabIndex != self.selectedIndex) {
    oldNavigationViewController = self.selectedViewController;
    if (![oldNavigationViewController isKindOfClass:UINavigationController.class]) {
      oldNavigationViewController = oldNavigationViewController.navigationController;
    }
    self.selectedIndex = tabIndex;
  }

  UINavigationController* toNavigationController = [self viewControllerAtIndex:tabIndex];
  if (![toNavigationController isKindOfClass:UINavigationController.class]) {
    toNavigationController = toNavigationController.navigationController;
  }
  
  if (toNavigationController.viewControllersCount > 1) {
    [toNavigationController popToRootViewControllerAnimated:NO];
  }
  if (toViewController != nil) {
    [toNavigationController pushViewController:toViewController
                                      animated:animated];
  }
  
  [oldNavigationViewController popToRootViewControllerAnimated:NO];
}

- (void)switchToTabIndex:(NSUInteger)tabIndex
     pushViewControllers:(NSArray*)viewControllers
                animated:(BOOL)animated{
  if (self.presentedViewController != nil) {
    [self dismissViewControllerAnimated:NO completion:nil];
  }
  UINavigationController* oldNavigationViewController = nil;
  if (tabIndex != self.selectedIndex) {
    oldNavigationViewController = self.selectedViewController;
    if (![oldNavigationViewController isKindOfClass:UINavigationController.class]) {
      oldNavigationViewController = oldNavigationViewController.navigationController;
    }
    self.selectedIndex = tabIndex;
  }
  
  UINavigationController* toNavigationController = [self viewControllerAtIndex:tabIndex];
  if (![toNavigationController isKindOfClass:UINavigationController.class]) {
    toNavigationController = toNavigationController.navigationController;
  }
  
  if (toNavigationController.viewControllersCount > 1) {
    [toNavigationController popToRootViewControllerAnimated:NO];
    
  }
  
  if (viewControllers != nil) {
    NSMutableArray* newViewControlers = [NSMutableArray array];
    if (toNavigationController.viewControllers) {
      [newViewControlers addObjectsFromArray:toNavigationController.viewControllers];
    }
    [newViewControlers addObjectsFromArray:viewControllers];
    [toNavigationController setViewControllers:newViewControlers animated:animated];
  }
  
  [oldNavigationViewController popToRootViewControllerAnimated:NO];
}

- (void)popAllToRootViewController {
  NSArray* viewControllers = self.viewControllers;
  for (UINavigationController* viewController in viewControllers) {
    if ([viewController isKindOfClass:UINavigationController.class]) {
      [viewController popToRootViewControllerAnimated:NO];
    }
  }
}

- (__kindof UIViewController*)topViewControllerAtIndex:(NSUInteger)tabIndex {
  UINavigationController* viewController = [self viewControllerAtIndex:tabIndex];
  if ([viewController isKindOfClass:UINavigationController.class]) {
    return viewController.topViewController;
  }
  return viewController;
}

- (__kindof UIViewController*)rootViewControllerAtIndex:(NSUInteger)tabIndex {
  UINavigationController* viewController = [self viewControllerAtIndex:tabIndex];
  if ([viewController isKindOfClass:UINavigationController.class]) {
    return viewController.rootViewController;
  }
  return viewController;
}

- (NSInteger)indexForViewController:(UIViewController*)viewController {
  return [self.viewControllers indexOfObject:viewController];
}

- (__kindof UIViewController*)selectedTopViewController {
  UINavigationController* viewController = self.selectedViewController;
  if ([viewController isKindOfClass:UINavigationController.class]) {
    return viewController.topViewController;
  }
  return viewController;
}

- (__kindof UIViewController*)selectedRootViewController {
  UINavigationController* viewController = self.selectedViewController;
  if ([viewController isKindOfClass:UINavigationController.class]) {
    return viewController.rootViewController;
  }
  return viewController;
}

#pragma mark - UIInterfaceOrientation

- (BOOL)shouldAutorotate {
  return self.selectedViewController.shouldAutorotate;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return self.selectedViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}

@end
